# -*- coding: utf-8 -*-

from __future__ import absolute_import, unicode_literals, print_function

import json
import re

import requests
from bs4 import BeautifulSoup
from lxml import etree
from lxml.etree import XML

from jiandan.exceptions import JianDanException
from jiandan.five import str_to_bytes
from jiandan.tools import get_elem_text, list_or_empty, replace_html, get_first_of_element, format_image_url

backgroud_image_p = re.compile('background-image:[ ]+url\(\"([\w\W]+?)\"\)')
js_content = re.compile('js_content.*?>((\s|\S)+)</div>')
find_article_json_re = re.compile('var msgList = (.*?)}}]};')
get_post_view_perm = re.compile('<script>var account_anti_url = "(.*?)";</script>')


class JianDanStructuring(object):

    @staticmethod
    def get_new_100_url(text):
        """new前100条数据（未获取到详细信息的）

        Parameters
        ----------
        text : str or unicode
            new100条数据
        Returns
        -------
        list[dict]
            {
                title:标题,
                url:详细url地址
            }
        """
        page = etree.HTML(text)
        lis = page.xpath('//div[@class="post f"]/ul/li')
        new_url_list = []
        for li in lis:
            url = get_first_of_element(li, 'a/@href')
            title = get_first_of_element(li, 'a/text()')
            new_url_list.append({
                'title': title,
                'url': url,
            })

        return new_url_list

    @staticmethod
    def get_new_url(text):
        """new每条数据（

        Parameters
        ----------
        text : str or unicode
            new每条数据
        Returns
        -------
        list[dict]
            {
                'author_name': 作者名称,
                'author_url': 作者_url,
                'create_datetime': 创建时间,
                'title': 标题,
                'content': 内容
            }
        """
        page = etree.HTML(text)
        lis = page.xpath('//div[@class="post f"][1]')
        new_list = []
        for li in lis:
            author_url = get_first_of_element(li, 'div[1]/strong/a/@href')
            author_name = get_first_of_element(li, 'div[1]/strong/a/text()')
            create_datetime = get_first_of_element(li, 'div[1]/text()')
            title = get_first_of_element(li, 'h1/a/text()')
            content_list = li.xpath('//div[@class="post f"][1]/p')
            content = []
            for item in content_list:
                content.append(get_first_of_element(item, 'text()'))
            new_list.append({
                'author_name': author_name,
                'author_url': author_url,
                'create_datetime': create_datetime,
                'title': title,
                'content': content
            })
        return new_list

    @staticmethod
    def get_ooxx_img(text):
        """ooxx妹子图的图片地址
        Parameters
        ----------
        text : str or unicode

        Returns
        -------
        list[dict]
            {
                'author_name': author_name,
                'image_src': image_src,
            }
        """
        page = etree.HTML(text)
        lis = page.xpath('//ol[@class="commentlist"]/li')
        image_list = []
        for li in lis:
            author_name = get_first_of_element(li, 'div/div/div/strong/text()')
            image_src = get_first_of_element(li, '/divdiv/div[2]/p/a/@href')
            image_list.append({
                'author_name': author_name,
                'image_src': image_src,
            })
        return image_list
